package com.wenmingliang.demo2;

import java.util.concurrent.*;

public class FutureAPIDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
        FutureTask futureTask = new FutureTask<String>( () ->{
            System.out.println(Thread.currentThread().getName() + "\t ------come in");
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "异步任务执行成功------------------------";
        });
        Thread t1 = new Thread(futureTask, "t1");
        t1.start();
//        System.out.println(futureTask.get(3, TimeUnit.SECONDS));
        System.out.println(Thread.currentThread().getName()+"\t--------忙其他任务了");

        while(true) {
            if (futureTask.isDone()) {
                System.out.println(futureTask.get());
                break;
            }else {
                try {
                    TimeUnit.MILLISECONDS.sleep(500);
                }catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("正在处理中=======================");
            }
        }
    }

}
